package com.api.fc.test;




import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.fc.FcGettoken;


public class test001 {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";


    public static void main(String[] args) {
        String  reqeustids="";
        String  userids="";

        reqeustids="964320";
        userids="243";

        String requestid[]=reqeustids.split(",");
        String userid[]=userids.split(",");
        //String  token = (String) testGetoken("http://oa.maxwell-gp.com.cn:8090").get("token");
        FcGettoken gt =new FcGettoken();
        String token=(String)gt.gettoken().get("token");

        for(int i=0;i<requestid.length;i++){
            HttpRequest.closeCookie();
            System.out.println(requestid[i]+"   "+userid[i]);
            String param="requestId="+requestid[i];
            String fanhui=testRestful(token,"http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/getRequestResources",param,userid[i]);
            System.out.println("最终返回:"+fanhui);




            Object succesResponse = JSON.parse(fanhui);    //先转换成Object
            Map map = (Map)succesResponse;
            if("SUCCESS".equals(map.get("code"))){
                JSONArray jsonArray = new JSONArray(map.get("mx"));
                for( int ii=0;ii<jsonArray.size();ii++){
                    JSONObject info = new JSONObject((Map<String, Object>) jsonArray.get(ii));
                    System.out.print("requestid:"+requestid[i]+"   ");
                    System.out.print("name:"+info.get("name")+"   ");
                    System.out.println("downloadUrl:"+info.get("downloadUrl"));

                }
            }
        }
    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

        String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);
        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }




    public static String testRestful(String token,String address,String api,String jsonParams,String userid){
        //ECOLOGY 返回的 token
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");

        JSONObject Endreturn = new JSONObject();

        //System.out.println(spk);

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64(userid,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口
        String data = HttpRequest.get(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                .body(jsonParams)
                .execute().body();


         System.out.println("token:"+token);
         System.out.println("userid:"+encryptUserid);

        System.out.println("testRestful()："+data);
        Object succesResponse = JSON.parse(data);
        Map map = (Map)succesResponse;

        Endreturn.put("code", map.get("code"));

        if("SUCCESS".equals(map.get("code"))){

            //JSONObject info = new JSONObject((Map<String, Object>) map.get("data"));
            //System.out.println(map.get("data"));

            JSONArray jsonArray = new JSONArray(map.get("data"));

            JSONArray fanhui = new JSONArray();

            for( int i=0;i<jsonArray.size();i++){
                JSONObject info = new JSONObject((Map<String, Object>) jsonArray.get(i));
                Map ee=new HashMap<String, Object>();
                ee.put("name",info.get("name"));
                ee.put("downloadUrl",info.get("downloadUrl"));
                //ee.put("name",info.get("name"));

                fanhui.put(ee);
                // System.out.print("name:"+info.get("name")+"   ");
                //System.out.println("downloadUrl:"+info.get("downloadUrl"));
            }

            Endreturn.put("mx", fanhui);

        }
        return Endreturn.toString();
    }



}

